package com.oa.hyperion.service.impl;

import com.oa.hyperion.Global;
import com.oa.hyperion.entity.Attendance;
import com.oa.hyperion.entity.Detail;
import com.oa.hyperion.entity.User;
import com.oa.hyperion.exception.http.ServerErrorException;
import com.oa.hyperion.repository.AttendanceMapper;
import com.oa.hyperion.repository.UserMapper;
import com.oa.hyperion.service.AttenDanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class AttenDanceServiceImpl implements AttenDanceService {
    @Autowired
    AttendanceMapper attendanceMapper;

    @Autowired
    UserMapper userMapper;
    @Value("${hyperion.Attendance.holidays}")
    private List<Integer> holidays;
    @Override
    public void CreateAttendance() {
        Calendar calendar=Calendar.getInstance();
        int today=calendar.get(Calendar.DAY_OF_YEAR);
        for (int i:holidays){
            if(today==i){
                throw new ServerErrorException(70007);
            }
        }
        List<User> users=userMapper.getAllUser();
        for (User user:users){
            int id=user.getId();
            attendanceMapper.createAttendance(id,new Date());
        }
    }

    @Override
    public Attendance SignIn(Integer employee) {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String createDate = format.format(new Date());

        Attendance attendance = attendanceMapper.findAttendance(employee, createDate);
        if(attendance==null){
            throw new ServerErrorException(70007);
        }
        if(attendance.getSignIn()!=null){
            throw new ServerErrorException(70001);
        }
        Date now=new Date();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(now);
        if(calendar.get(Calendar.HOUR_OF_DAY)>18){
            throw new ServerErrorException(70002);
        }
        attendanceMapper.signIn(now,employee,createDate);
        Attendance attendance2 = attendanceMapper.findAttendance(employee, createDate);
        if(attendance2.getSignIn()==null){
            throw new ServerErrorException(70003);
        }
        return attendance2;
    }

    @Override
    public Attendance SignOut(Integer employee) {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String createDate = format.format(new Date());
        Attendance attendance = attendanceMapper.findAttendance(employee, createDate);
        if(attendance==null){
            throw new ServerErrorException(70007);
        }
        if(attendance.getSignOut()!=null){
            throw new ServerErrorException(70004);
        }

        Date now=new Date();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(now);
        if(calendar.get(Calendar.HOUR_OF_DAY)<18){
            throw new ServerErrorException(70005);
        }
        attendanceMapper.signOut(now,employee,createDate);

        Attendance attendance2 = attendanceMapper.findAttendance(employee, createDate);
        if(attendance2.getSignOut()==null){
            throw new ServerErrorException(70006);
        }
        return attendance2;
    }

    @Override
    public void AttendanceJudge() {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String createDate = format.format(new Date());
        List<Attendance> attendanceList = attendanceMapper.findAttendanceList(createDate);
        if(attendanceList.size()==0){
            throw new ServerErrorException(70007);
        }
        for (Attendance a:attendanceList){
            //是否请假
//            Detail vacation = attendanceMapper.isVacation(createDate, a.getEmployee());
//            if(vacation!=null){
//                attendanceMapper.jduge("v",a.getEmployee(),createDate);
//                continue;
//            }

            Calendar c1=Calendar.getInstance();
            Calendar c2=Calendar.getInstance();
            //无签到,旷工
            if(a.getSignIn()==null||a.getSignOut()==null){
                attendanceMapper.jduge("z",a.getEmployee(),createDate);
                continue;
            }
            c1.setTime(a.getSignIn());
            Calendar signInTime=Calendar.getInstance();
            signInTime.set(c1.get(Calendar.YEAR),c1.get(Calendar.YEAR),c1.get(Calendar.YEAR), Global.signInHour, Global.signInMin, Global.signInSecond);
            c2.setTime(a.getSignOut());
            Calendar signOutTime=Calendar.getInstance();
            signInTime.set(c2.get(Calendar.YEAR),c2.get(Calendar.YEAR),c2.get(Calendar.YEAR), Global.signOUtHour, Global.signOutMin,Global.signOutSecond);
            if(c1.before(signInTime)&&c2.after(signOutTime)){
                attendanceMapper.jduge("y",a.getEmployee(),createDate);
            }else {
                attendanceMapper.jduge("x",a.getEmployee(),createDate);
            }
        }
    }

    @Override
    public List<Attendance> AttendanceExceptionSelect(Attendance attendance) {
        if(attendance.getUser()!=null&&attendance.getUser().getAccount()!=null){
            User user = userMapper.getUserByAccount(attendance.getUser().getAccount());
            if (user!=null){
                attendance.setEmployee(user.getId());
            }else {
                return null;
            }
        }
        return attendanceMapper.exceptionRecord(attendance);
    }

    @Override
    public void Supplementary(Attendance attendance) {
        if(!(attendance.getStatus()!=null&&(attendance.getStatus().equals("缺勤")||attendance.getStatus().equals("旷工")))){
            throw new ServerErrorException(70008);
        }
        Calendar c=Calendar.getInstance();
        c.setTime(attendance.getCreateDate());
        c.add(Calendar.HOUR_OF_DAY,24);
        Date date=c.getTime();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String createDate = format.format(date);
        attendanceMapper.jduge("s",attendance.getEmployee(),createDate);
    }
}
